<?php
require_once dirname(__FILE__)."/../config/dbconn.php";
  
class Categories extends Dbconn {
    //выборка всех категорий
    function show_categories(){
        $sql = Categories::getConnect()->query("
                 SELECT id, 
                        cat_name, 
                        year
                 FROM category
                 ORDER BY id DESC");
        return $sql;
    }
    
    //создание категории
    function add_category($name){
        $date = date('Y');
        $sql = Categories::getConnect()->prepare("
                 INSERT INTO category(
                                      cat_name,
                                      year)
                 VALUES(
                        :name,
                        :date)
                 ");
        $sql->bindParam(':name', $name);
        $sql->bindParam(':date', $date);
        $sql->execute();
    }
    
    //редактирование категории
    function update_category($id, $name){
        $sql = Categories::getConnect()->prepare("
                 UPDATE category
                 SET cat_name = :name
                 WHERE id = :id
                 ");
        $sql->bindParam(':name', $name);
        $sql->bindParam(':id', intval($id));
        $sql->execute();
    }
    
    //удаление категории
    function delete_category($id){
        $sql = Categories::getConnect()->prepare("
                 DELETE FROM category
                 WHERE id = :id
                 ");
        $sql->bindParam(':id', intval($id), PDO::PARAM_INT);
        $sql->execute();
    }
    
    //получения кол-ва записей в таблице
    function categories_count(){
        $sql = Categories::getConnect()->query("
                 SELECT count(id) 
                 FROM category
                 ");        
        return $sql;
    }
    
    function show_admin_categories($start, $num){
        $sql = Categories::getConnect()->prepare("
                 SELECT id,
                        cat_name, 
                        year
                 FROM category
                 ORDER BY id DESC
                 LIMIT ".$start.", 
                       ".$num
                 );
        $sql->execute();
        return $sql;
    }
    
    function sort_categories($sort){
        $sql = Categories::getConnect()->prepare("
                 SELECT id, 
                        cat_name, 
                        year
                 FROM category
                 ORDER BY :sort DESC
                 ");
        $sql->bindParam(':sort', $sort);         
        $sql->execute();
        return $sql;
    } 
}  

